package com.lyf.baoxian.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by 刘亚飞 on 2016/11/30.
 * 首页ViewPager
 */

public class MyViewPagerAdapter extends PagerAdapter {
    private int[] imagesIds;

    public MyViewPagerAdapter(int[] imagesIds) {
        this.imagesIds = imagesIds;
    }

    /**
     * 返回有多少页,返回一个最大值，实现无限轮播
     * @return
     */
    @Override
    public int getCount() {
        return imagesIds.length * 10000 * 100;
    }

    /**
     * 判断VIewPager是否复用
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 创建item
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(container.getContext());
        //每个imageView的位置
        position = position % imagesIds.length;
        imageView.setImageResource(imagesIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        //加入到容器中
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
